﻿using MyMVC.BLL;
using MyMVC.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace MyMVC.Controllers
{
    public class HomeController : Controller
    {
        EmployeeManager em = new EmployeeManager();

        /// <summary>
        /// 返回查询数据
        /// </summary>
        /// <returns></returns>
        public ActionResult List() {
            List<Employee> le = em.GetList();
            return View(le);
        }

        /// <summary>
        /// 返回查询已删除数据
        /// </summary>
        /// <returns></returns>
        public ActionResult List2()
        {
            List<Employee> le = em.GetList2();
            return View(le);
        }

        /// <summary>
        /// 调用删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult del(int id = 0) {
            if (id > 0) {
                if (em.del(id)) {
                    return Content("<script>alert('删除成功！');window.location='" + Url.Action("List") + "'</script>");
                }
            }
            return Content("<script>alert('删除失败！');window.location='" + Url.Action("List") + "'</script>");
        }

        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult huanyuan(int id = 0)
        {
            if (id > 0)
            {
                if (em.huanyuan(id))
                {
                    return Content("<script>alert('还原成功！');window.location='" + Url.Action("List") + "'</script>");
                }
            }
            return Content("<script>alert('还原失败！');window.location='" + Url.Action("List") + "'</script>");
        }

        /// <summary>
        /// 跳转添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult toadd() {
            return View("add");
        }

        /// <summary>
        /// 调用添加
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public ActionResult add(Employee e,string u) {

            //上传图片
            //获取image值
            HttpPostedFileBase file = Request.Files["image"];
            if (file != null)
            {
                try
                {
                    //指定存放文件夹
                    var filename = Path.Combine(Request.MapPath("~/Image"), file.FileName);
                    //保存上载文件的内容
                    file.SaveAs(filename);
                }
                catch (Exception ex)
                {
                    return Content(string.Format("上传文件出现异常：{0}", ex.Message));
                }
            }

            //接收图片路径
            u = file.FileName;

            if (ModelState.IsValid)
            {
                //将图片路径传值过去
                if (em.add(e,u))
                {
                    return RedirectToAction("List");
                }
            }

            return View("List");

        }

        /// <summary>
        /// 跳转个人中心页面
        /// </summary>
        /// <returns></returns>
        public ActionResult toAccount() {
            return View("Account");
        }

        /// <summary>
        /// 跳转修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult update(int id=0) {
            List<Employee> le = em.getlistid(id);
            return View(le);
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="emp">数据信息</param>
        /// <param name="u">图片路径</param>
        /// <param name="id">传的id</param>
        /// <returns></returns>
        public ActionResult doupdate(Employee emp,string u,int id=0)
        {
            int a = em.update(emp, u, id);
            if (a > 0)
            {
                return Content("<script>alert('修改成功！');window.location='" + Url.Action("List") + "'</script>");
            }
            return Content("<script>alert('修改失败！');</script>");
        }

    }
}